//
//  GeRenViewController.m
//  XiangMu
//
//  Created by 康世标 on 15/8/12.
//  Copyright (c) 2015年 康世标. All rights reserved.
//

#import "GeRenViewController.h"

@interface GeRenViewController ()

@end

@implementation GeRenViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    
    NSString * name = [user objectForKey:@"nickname"];
    
    NSString * phoneNum = [user objectForKey:@"phoneNum"];
    //1 男
    NSString * p = [[user objectForKey:@"sex"] intValue]==1?@"男":@"女";
    
   
    NSLog(@"---%@",[user objectForKey:@"birthday"]);
    NSString * birthdy =[NSString stringWithFormat:@"%@",[user objectForKey:@"birthday"]];
    if ([birthdy  isEqualToString:@""]) {
        
        NSString * date = @"2000-01-01";
        _textArr = [[NSMutableArray alloc]initWithObjects:name,phoneNum,p,date,nil];;
    }
    else{
         NSString * date = [self timer:[[user objectForKey:@"birthday"] longValue]];
            _textArr = [[NSMutableArray alloc]initWithObjects:name,phoneNum,p,date,nil];;
    }
  

    [self.tableView reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
     self.tabBarController.tabBar.hidden=YES;
    UIButton *fanHuiBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    fanHuiBtn.frame=CGRectMake(20, 20, 10, 20);
    
    [fanHuiBtn setBackgroundImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    
    [fanHuiBtn addTarget:self action:@selector(FanHui) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *barFanHui=[[UIBarButtonItem alloc]initWithCustomView:fanHuiBtn];
    self.navigationItem.leftBarButtonItem=barFanHui;
    
    _titArr=[[NSMutableArray alloc ]initWithObjects:@"头像:",@"用户名:", @"账号:", @"性别:", @"出生日期:",  nil];
    
  
    
    [self.tableView registerNib:[UINib nibWithNibName:@"TouXangTableViewCell" bundle:nil] forCellReuseIdentifier:@"youxiang"];
    
   

    _datePicker.datePickerMode = UIDatePickerModeDate;

    _datePicker.maximumDate = [NSDate date];
    
    self.view1.hidden = YES;
    self.imag1 = [UIImage imageNamed:@"like"];
    
}
#pragma  mark --- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        
        TouXangTableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:@"youxiang" forIndexPath:indexPath];
        
        [cell.imagView1 sd_setImageWithURL:[NSURL URLWithString:[[NSUserDefaults standardUserDefaults] objectForKey:@"headImg"]] placeholderImage:[UIImage imageNamed:@"like"]];
        
        return cell;
        
    }
    else
    {
    static NSString * geren=@"";
    
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:geren];
    
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:geren];
        
    }
        if (indexPath.row!=2) {
            
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        }
        
        cell.textLabel.text=[_titArr objectAtIndex:indexPath.row];
        
      
            
        cell.detailTextLabel.text = [_textArr objectAtIndex:indexPath.row-1];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
        
      
    return cell;
    }
}
#pragma mark-----UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.row) {
        case 0:
        {
            UIActionSheet * sheet=[[UIActionSheet alloc]initWithTitle:@"温馨提示" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册中选", nil];
            [sheet showInView:self.view];
        }
            break;
        case 1:
        {
            XiuGaiYongHuMingViewController * xiu =[[XiuGaiYongHuMingViewController alloc] init];
        
//            xiu.Myblock=^(NSString *name){
//                
//            };

            [self.navigationController pushViewController:xiu animated:YES];
            
        }
            break;
        case 2:
        {
            
        }
            break;
        case 3:
        {
            XiuGaiXingBieViewController *x =[[XiuGaiXingBieViewController alloc]init];
            
            [self.navigationController pushViewController:x animated:YES];
            
        }
            break;
        case 4:
        {
             self.view1.hidden = NO;
        }
            break;
        default:
            break;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row==0) {
        
        return 47;
    }
    return 44;
}
#pragma  mark---UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
       NSInteger type =0;
    switch (buttonIndex) {
            
       
        case 0:
            
        {     type=UIImagePickerControllerSourceTypeCamera;
            if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
            {
                [[[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"摄像头坏了" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]show];
                return;
            }
        }
            break;
        case 1:
        {
            type=UIImagePickerControllerSourceTypePhotoLibrary;
        }
            break;
        case 2:
        {
             NSLog(@"取消");
            return;
        }
            break;

        default:
            break;
    }
    UIImagePickerController * picke=[[UIImagePickerController alloc]init];
    
    picke.delegate =self;
    picke.sourceType=type;
    picke.allowsEditing = YES;
    [self presentViewController:picke animated:YES completion:nil];
    
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
   
    [self dismissViewControllerAnimated:YES completion:nil];
    
    
    UIImage * newImage = [self imageWithImageSimple:image scaledToSize:CGSizeMake(kScreenW, kScreenH)];
    
    [self upDateImag:newImage];
//    self.imag1 = image;
//    [self.tableView reloadData];
    
}
//上传头像
- (void)upDateImag:(UIImage *)image{
    
    MBProgressHUD * hud =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [QingQiuShuJu ShangChuanImagView:image ID:[[NSUserDefaults standardUserDefaults] objectForKey:@"ID"]Block:^(NSDictionary *dic, BOOL isOK){
        
        if (isOK) {
            
            if ([[dic valueForKey:@"error"] intValue]==0) {
                hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
                
                // Set custom view mode
                hud.mode = MBProgressHUDModeCustomView;
                
                NSUserDefaults * use =[NSUserDefaults standardUserDefaults];
                
                [use setObject:[dic valueForKey:@"url"] forKey:@"headImg"];
                
                
                hud.labelText = @"修改成功";
                
                [hud show:YES];
                [hud hide:YES afterDelay:2];
                [self viewWillAppear:YES];
                [self.tableView reloadData];
            
                
            }
            else{
                
                hud.mode = MBProgressHUDModeText;
                
                hud.labelText = @"上传失败";
                
                [hud hide:YES afterDelay:2];
            }
        
        }
        else{
            hud.mode = MBProgressHUDModeText;
            
            hud.labelText = @"网络连接失败";
            
            [hud hide:YES afterDelay:2];

            
        }
        
    }];
    
}
-(void)FanHui{
    self.tabBarController.tabBar.hidden=NO;
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//- (void)datePickerChang1:(UIDatePicker *)sender{
//    
//    UIDatePicker* control = (UIDatePicker*)sender;
//    NSDate* qdate = control.date;
//    
//    NSLog(@"----%@",qdate);
//    
//}
- (IBAction)queDing:(UIButton *)sender{
    
    NSLog(@"----%@",_datePicker.date);
    
    NSDate * date = _datePicker.date;
    
    NSDateFormatter * forma = [[NSDateFormatter alloc]init];
    
    [forma setDateFormat:@"yyyy-MM-dd"];
    
    NSString * string = [forma stringFromDate:date];
    
    
    [self update:string];
    
    
     self.view1.hidden = YES;
}
- (IBAction)quXiao:(UIButton *)sender{
     self.view1.hidden = YES;
}

- (NSString *)timer:(long )time{
    
    NSDate * date = [NSDate dateWithTimeIntervalSince1970:time/1000];
    
    NSDateFormatter * formatter =[[NSDateFormatter alloc]init];
    
    [formatter setDateFormat:@"yyyy-MM-dd"];
    
    NSString * string =[formatter stringFromDate:date];
    
    
    return string;
}

- (void)update:(NSString *)timer{
    
    MBProgressHUD * HUD =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    
    [QingQiuShuJu upDate:@(3) Param:timer Block:^(NSDictionary *dic, BOOL isOK) {
        
        if (isOK) {
            
            NSUserDefaults * use =[NSUserDefaults standardUserDefaults];
            [use setObject:[[dic valueForKey:@"data"] valueForKey:@"birthday"]forKey:@"birthday"];
            
            HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
            
            // Set custom view mode
            HUD.mode = MBProgressHUDModeCustomView;
            
            
            HUD.labelText = @"修改成功";
            
            [HUD show:YES];
            [HUD hide:YES afterDelay:3];
            [self viewWillAppear:YES];
            
            
            [self.tableView reloadData];
            
            
        }
        else{
            
            HUD.labelText = @"请求失败";
            
            [HUD hide:YES afterDelay:2];
            HUD.removeFromSuperViewOnHide = YES;
            
        }
    }];
}
//压缩图片质量
-(UIImage *)reduceImage:(UIImage *)image percent:(float)percent
{
    NSData *imageData = UIImageJPEGRepresentation(image, percent);
    UIImage *newImage = [UIImage imageWithData:imageData];
    return newImage;
}
- (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
    // Create a graphics image context
    UIGraphicsBeginImageContext(newSize);
    // new size
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    // Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // End the context
    UIGraphicsEndImageContext();
    // Return the new image.
    return newImage;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
